﻿using System.Linq;
using DAL;

namespace BLL
{
    public class UserThingBll
    {
        private readonly CourseInfo _courseInfo;
        private readonly UserThing _userThing;

        /// <summary>
        /// 用户评分——含金量
        /// </summary>
        public int MyGold
        {
            get { return _userThing.Gold; }
        }

        /// <summary>
        /// 用户评分——轻松度
        /// </summary>
        public int MyRelax
        {
            get { return _userThing.Relax; }
        }

        /// <summary>
        /// 用户评分——成绩值
        /// </summary>
        public int MyMark
        {
            get { return _userThing.Mark; }
        }

        /// <summary>
        /// 根据用户ID，课程ID和课程信息构造对象
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="courseId">课程在数据库中的ID</param>
        /// <param name="courseInfo">课程信息</param>
        public UserThingBll(string userId, string courseId, CourseInfo courseInfo)
        {
            using (var context = new ModelContainer())
            {
                _userThing =
                    context.UserThings.SingleOrDefault(thing => thing.CourseId == courseId && thing.UserId == userId)
                    ?? new UserThing { CourseId = courseId, UserId = userId };
            }
            _courseInfo = courseInfo;
        }

        /// <summary>
        /// 返回课程是否已被推荐
        /// </summary>
        /// <returns>判断结果</returns>
        public bool HasRecommended()
        {
            return _userThing.HasRcmd;
        }

        /// <summary>
        /// 推荐课程，写入数据库
        /// </summary>
        /// <returns>新的推荐数量</returns>
        public int Recommend()
        {
            _userThing.HasRcmd = true;
            _courseInfo.RcmdNum++;
            if (_userThing.ChangeTracker.State != ObjectState.Added)
                _userThing.MarkAsModified();
            _courseInfo.MarkAsModified();
            using (var context = new ModelContainer())
            {
                context.UserThings.ApplyChanges(_userThing);
                context.CourseInfos.ApplyChanges(_courseInfo);
                context.SaveChanges();
                _userThing.AcceptChanges();
                _courseInfo.AcceptChanges();
            }
            return _courseInfo.RcmdNum;
        }

        /// <summary>
        /// 返回用户是否已评分
        /// </summary>
        /// <returns>判断结果</returns>
        public bool HasScored()
        {
            return _userThing.HasScored;
        }

        /// <summary>
        /// 评分，写入数据库
        /// </summary>
        /// <param name="gold">含金量</param>
        /// <param name="relax">轻松度</param>
        /// <param name="mark">给分值</param>
        public void Score(int gold, int relax, int mark)
        {
            _userThing.Gold = gold;
            _userThing.Relax = relax;
            _userThing.Mark = mark;
            _userThing.HasScored = true;
            _courseInfo.Gold = (_courseInfo.Gold * _courseInfo.ScoredNum + gold) / (_courseInfo.ScoredNum + 1);
            _courseInfo.Relax = (_courseInfo.Relax * _courseInfo.ScoredNum + relax) / (_courseInfo.ScoredNum + 1);
            _courseInfo.Mark = (_courseInfo.Mark * _courseInfo.ScoredNum + mark) / (_courseInfo.ScoredNum + 1);
            _courseInfo.ScoredNum++;
            _userThing.MarkAsModified();
            _courseInfo.MarkAsModified();
            using (var context = new ModelContainer())
            {
                context.UserThings.ApplyChanges(_userThing);
                context.CourseInfos.ApplyChanges(_courseInfo);
                context.SaveChanges();
                _userThing.AcceptChanges();
                _courseInfo.AcceptChanges();
            }
        }

        /// <summary>
        /// 增加一条访问记录
        /// </summary>
        public void Visit()
        {
            _userThing.Visits++;
            if (_userThing.ChangeTracker.State != ObjectState.Added)
                _userThing.MarkAsModified();
            using (var context = new ModelContainer())
            {
                context.UserThings.ApplyChanges(_userThing);
                context.CourseInfos.ApplyChanges(_courseInfo);
                context.SaveChanges();
                _userThing.AcceptChanges();
                _courseInfo.AcceptChanges();
            }
        }

        public static string GetPic(string id)
        {
            using (var context = new ModelContainer())
            {
                var userThing = context.AllThings.Where(a => a.UserId == id).FirstOrDefault();
                if (userThing != null)
                    return userThing.UserPic;
                return "";
            }
        }
    }
}
